package com.wlw.admin.sample;

import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.wlw.admin.pluginlib.dynamicload.internal.DLIntent;
import com.wlw.admin.pluginlib.dynamicload.internal.DLPluginManager;
import com.wlw.admin.pluginlib.utils.DLUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements OnItemClickListener {
    private static final String FROM = "extra.from";
    private static final int FROM_INTERNAL = 0;
    private static final int FROM_EXTERNAL = 1;

    private TextView tvNoPlugin;
    private RecyclerView recyclerView;
    private List<PluginItem> pluginItems = new ArrayList<>();
    private PluginAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
    }

    private void initData() {
        String pluginFolder = Environment.getExternalStorageDirectory() + "/Download";
        File file = new File(pluginFolder);
        File[] files = file.listFiles();
        if (files == null || files.length == 0) {
            tvNoPlugin.setVisibility(View.VISIBLE);
            return;
        }
        for (File plugin : files) {
            if(plugin.isFile()){
                PluginItem item = new PluginItem();
                item.pluginPath = plugin.getAbsolutePath();

                item.apkSize =plugin.length();
                item.packageInfo = DLUtils.getPackageInfo(this, item.pluginPath);
                if (item.packageInfo != null && item.packageInfo.activities != null && item.packageInfo.activities.length > 0) {
                    item.launcherActivityName = item.packageInfo.activities[0].name;
                }
                if (item.packageInfo != null && item.packageInfo.services != null && item.packageInfo.services.length > 0) {
                    item.launcherServiceName = item.packageInfo.services[0].name;
                }
                pluginItems.add(item);
                DLPluginManager.getInstance(this).loadApk(item.pluginPath,false);
            }
        }
        adapter.notifyDataSetChanged();
    }

    private void initView() {
        tvNoPlugin = findViewById(R.id.tv_no_plugin);
        recyclerView = findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new PluginAdapter(pluginItems);
        recyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(int position) {
        PluginItem item = pluginItems.get(position);
        DLPluginManager pluginManager = DLPluginManager.getInstance(this);
        pluginManager.startPluginActivity(this,new DLIntent(item.packageInfo.packageName,item.launcherActivityName));
        Log.e("startPluginActivity","aaaaaaaaa");
        if (item.launcherServiceName!=null){
            DLIntent intent=new DLIntent(item.packageInfo.packageName,item.launcherActivityName);

        }
    }
}
